<?php

namespace App\Kuafu\Mybank\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Shop\Model\Shop;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;

class MybankBillRecordModel extends BaseModel
{

    use HasFactory, KfSoftDeletes, KfUid;

    protected $connection = 'mysql';
    protected $table      = "mybank_bill_record";

    protected $fillable = [
        'id',
        'shopId',
        'source',
        'transId',
        'transDate',
        'transType',
        'displayTransType',
        'transStatus',
        'transDesc',
        'transAmount',
        'afterRemainingAmount',
        'remark',
        'flagMark',
        'billDate',
        'flagDivision',
        'billRecordBindId'
    ];

    const SOURCE_MEITUAN = 1;
    const SOURCE_ELE     = 2;
    const SOURCE_CANTEEN = 3;
    const SOURCE_DOUYIN  = 4;

    const MAPPING_SOURCE = [
        self::SOURCE_MEITUAN => '美团',
        self::SOURCE_ELE     => '饿了么',
        self::SOURCE_CANTEEN => '堂食',
        self::SOURCE_DOUYIN  => '抖音',
    ];

    const DEFAULT_TRANS_TYPE  = 0;
    const WITHDRAW_TRANS_TYPE = 1;
    const PAY_TRANS_TYPE      = 2;
    const INCOME_TRANS_TYPE   = 3;

    const MAPPING_TRANS_TYPE = [
        self::DEFAULT_TRANS_TYPE  => '未知',
        self::WITHDRAW_TRANS_TYPE => '提现支出',
        self::PAY_TRANS_TYPE      => '其他支出',
        self::INCOME_TRANS_TYPE   => '收入',
    ];

    const DEFAULT_FLAG_MARK = 0;
    const MATCH_FLAG_MARK   = 1;

    const MAPPING_FLAG_MARK = [
        self::DEFAULT_FLAG_MARK => '未匹配',
        self::MATCH_FLAG_MARK   => '已匹配',
    ];

    const DEFAULT_FLAG_DIVISION = 0;
    const MATCH_FLAG_DIVISION   = 1;

    const MAPPING_FLAG_DIVISION = [
        self::DEFAULT_FLAG_DIVISION => '待分账',
        self::MATCH_FLAG_DIVISION   => '分账完成',
    ];

    const TRANS_STATUS_DEFAULT = 0;//转账状态 0 未知 1 成功 2 失败
    const TRANS_STATUS_SUC     = 1;//转账状态 0 未知 1 成功 2 失败
    const TRANS_STATUS_FAIL    = 2;//转账状态 0 未知 1 成功 2 失败

    const MAPPING_TRANS_STATUS = [
        self::TRANS_STATUS_DEFAULT => '未知',
        self::TRANS_STATUS_SUC     => '成功',
        self::TRANS_STATUS_FAIL    => '失败',
    ];

    /**
     * @return HasOne
     */
    public function shopRel(): HasOne
    {
        return $this->hasOne(Shop::class, "id", "shopId");
    }

    /**
     * 获取外卖平台订单数据-最新一笔
     * @param int $transType
     * @param int $flagMark
     * @param string $startTime
     * @param int $source
     * @return array
     */
    public static function getBillRecordGroupShopIds(
        int $transType,
        int $flagMark,
        string $startTime,
        int $source
    ): array {
        return self::query()
                   ->when($transType >= 0, function ($query) use ($transType) {
                       $query->where('transType', $transType);
                   })
                   ->when($flagMark >= 0, function ($query) use ($flagMark) {
                       $query->where('flagMark', $flagMark);
                   })
                   ->when($startTime, function ($query) use ($startTime) {
                       $query->where('transDate', '<', $startTime);
                   })
                   ->when(is_int($source) && $source, function ($query) use ($source) {
                       $query->where('source', $source);
                   })
                   ->where('transStatus', self::TRANS_STATUS_SUC)//转账状态 0 未知 1 成功 2 失败
                   ->orderBy('id')
                   ->groupBy('shopId')
                   ->get()
                   ->toArray();
    }

    /**
     * 获取外卖平台订单数据
     * @param int $shopId
     * @param int $transType
     * @param int $flagMark
     * @param string $startTime
     * @param int $source
     * @param int $startDate
     * @param int $endDate
     * @return array
     */
    public static function getBillRecordList(
        int $shopId,
        int $transType,
        int $flagMark,
        string $startTime,
        int $source,
        int $startDate = 0,
        int $endDate = 0
    ): array {
        return self::query()
                   ->when($shopId, function ($query) use ($shopId) {
                       $query->where('shopId', $shopId);
                   })
                   ->when($transType >= 0, function ($query) use ($transType) {
                       $query->where('transType', $transType);
                   })
                   ->when($flagMark >= 0, function ($query) use ($flagMark) {
                       $query->where('flagMark', $flagMark);
                   })
                   ->when($startTime, function ($query) use ($startTime) {
                       $query->where('transDate', '<', $startTime);
                   })
                   ->when(is_int($source) && $source, function ($query) use ($source) {
                       $query->where('source', $source);
                   })
                   ->when($startDate && $endDate, function ($query) use ($startDate, $endDate) {
                       $query->whereBetween('transDate', [$startDate, $endDate]);
                   })
                   ->where('transStatus', self::TRANS_STATUS_SUC)//转账状态 0 未知 1 成功 2 失败
                   ->orderBy('id')
                   ->get()
                   ->toArray();
    }

    /**
     * 获取外卖平台订单总金额
     * @param int $shopId
     * @param int $transType
     * @param int $flagMark
     * @param int $source
     * @param int $startDate
     * @param int $endDate
     * @return int|mixed
     */
    public static function getBillRecordTotalAmount(
        int $shopId,
        int $transType,
        int $flagMark,
        int $source,
        int $startDate = 0,
        int $endDate = 0
    ): int {
        return self::query()
                   ->when($shopId, function ($query) use ($shopId) {
                       $query->where('shopId', $shopId);
                   })
                   ->when($transType >= 0, function ($query) use ($transType) {
                       $query->where('transType', $transType);
                   })
                   ->when($flagMark >= 0, function ($query) use ($flagMark) {
                       $query->where('flagMark', $flagMark);
                   })
                   ->when(is_int($source) && $source, function ($query) use ($source) {
                       $query->where('source', $source);
                   })
                   ->when($startDate && $endDate, function ($query) use ($startDate, $endDate) {
                       $query->whereBetween('transDate', [$startDate, $endDate]);
                   })
                   ->where('transStatus', self::TRANS_STATUS_SUC)//转账状态 0 未知 1 成功 2 失败
                   ->sum('transAmount');
    }

}
